//
//  ReadExcelPage.swift
//  ReportScan
//
//  Created by woniu on 2025/11/6.
//
//读取Excel表格数据
import SwiftUI
import SwiftXLSX

struct ReadExcelPage: View {
    
    @State var fileUrl:URL?
    
    var body: some View {
        Text("读取Excel表格数据（需要把表格转成csv格式）")
        
        VStack(alignment: .leading) {
            Button(role: .none) {
                onChoseExcelFile()
            } label: {
                Text("选择Excel的csv文件")
            }
        }
        .padding()
        Spacer()
    }
    
    private func onChoseExcelFile(){
        let openPanel = NSOpenPanel()
        openPanel.title = "选择CSV文件"
        openPanel.showsHiddenFiles = false
        openPanel.allowsMultipleSelection = false
        openPanel.canChooseDirectories = false
        openPanel.canChooseFiles = true
        
        openPanel.begin {result in
            if result == .OK, let url = openPanel.url {
                fileUrl = url
                print("选择文件:\(url)")
                readExcelData(fileUrl: url)
            }
        }
    }
    
    //读取表格内容
    private func readExcelData(fileUrl:URL){
        let list = AppUtil.getCSVFile(isDropFirst: true, fileUrl: fileUrl)
        print(list)
    }
}

#Preview {
    ReadExcelPage()
}
